package com.own.business.push.persistence.store.method;

import com.alibaba.fastjson2.JSON;
import com.own.business.push.core.base.BaseRepeatRegistrationIdMethod;
import com.own.business.push.core.base.BaseSaveRegistrationIdMethod;
import com.own.business.push.core.model.RegisterRegistrationIdModel;
import com.own.component.store.core.Store;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * DefaultRegistrationIdMethodImpl
 *
 * @author chenxueli
 * @date 2023-11-14 10:51:00
 */
@Slf4j
@Component
public class PersistenceStoreSaveRegistrationIdMethod implements BaseSaveRegistrationIdMethod {

    @Resource
    private Store<String> store;
    @Resource
    private BaseRepeatRegistrationIdMethod baseRepeatRegistrationIdMethod;

    /**
     * 保存注册id
     *
     * @param model 注册id模型
     */
    @Override
    public void save(RegisterRegistrationIdModel model) {
        var key = buildKey(model.getUserId());
        var oldRegistrationId = store.value().get(key);
        // 判断是否已经存在了，并且和现在的不一样
        if (StringUtils.isNotBlank(oldRegistrationId)) {
            if (oldRegistrationId.equals(model.getRegistrationId())) {
                return;
            }
            var flag = baseRepeatRegistrationIdMethod.run(model, oldRegistrationId);
            if (!flag) {
                return;
            }
        }
        store.value().set(key, model.getRegistrationId());
        store.map().put("push:r:" + model.getModule(), model.getRegistrationId(), model.getUserId() + "");
    }

    /**
     * 根据用户id删除
     *
     * @param userId 用户id
     */
    @Override
    public void removeByUserId(Long userId) {
        var model = getByUserId(userId);
        if (model == null) {
            return;
        }
        var key = buildKey(userId);
        store.value().remove(key);
        store.map().remove("push:r:" + model.getModule(), model.getRegistrationId());
    }

    /**
     * 根据用户id获取注册id
     *
     * @param userId 用户id
     * @return 注册id
     */
    @Override
    public RegisterRegistrationIdModel getByUserId(Long userId) {
        var key = buildKey(userId);
        var content = store.value().get(key);
        if (StringUtils.isBlank(content)) {
            return null;
        }
        return JSON.parseObject(content, RegisterRegistrationIdModel.class);
    }

    /**
     * 根据用户id列表获取注册id列表
     *
     * @param userIdList 用户id列表
     * @return 注册id列表
     */
    @Override
    public List<RegisterRegistrationIdModel> listByUserIdList(Collection<Long> userIdList) {
        return userIdList.stream().map(this::getByUserId).collect(Collectors.toList());
    }

    /**
     * 根据模块查询注册id列表
     *
     * @param module 模块
     * @return 注册id列表
     */
    @Override
    public List<String> listRegIdListByModule(String module) {
        var map = store.map().get("push:r:" + module);
        if (map == null || map.isEmpty()) {
            return new ArrayList<>();
        }
        return new ArrayList<>(map.keySet());
    }

    private String buildKey(Long userId) {
        return "push:r:" + userId;
    }

}
